import BTControlNode from "../BTControlNode";
import { BTResult, RUNNING } from "../BTEnum";

export default class BTRandom extends BTControlNode {
    private _index: number = 0;
    private _result: BTResult;

    start() {
        this._index = Math.floor(Math.random() * this.children.length);
        this.children[this._index].start();
    }

    update() {
        this._result = this.children[this._index].update();
        if (this._result !== RUNNING) {
            this.children[this._index].end();
        }
        return this._result;
    }

    end(): void {
        if (this._result === RUNNING) {
            this.children[this._index].end();
        }
    }
}
